package com.tis.std.batch.construction.lowrise;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jpa.entitys.promis.construction.CostTxnStatusHistory;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.QueueProcessAction;
import apps.TransactionHistorysService;
import apps.sessionFactory;

import com.tis.std.commons;

public class ContractBatch  extends commons{
	
	private static final Logger log = Logger.getLogger(ContractBatch.class);
	
	private void BatchExec() throws Exception {
		try{
			
			StringBuilder _strQuery = new StringBuilder();
			
			_strQuery.append("  ");
			_strQuery.append(" SELECT "); 
			_strQuery.append(" COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
			_strQuery.append(" FROM SPL.VW_SYNC_CONTRACTS ");
			_strQuery.append(" WHERE SYNC_ISSTAGING = 'N'  ");
			_strQuery.append(" AND STATUS IN ('A','Z','C', 'X')  ");
			_strQuery.append(" AND APPROVED_BY IS NOT NULL  ");
			_strQuery.append(" GROUP BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
			_strQuery.append(" ORDER BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON ");
		
			/*
			_strQuery.append(" SELECT ");
			_strQuery.append(" COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON "); 
			_strQuery.append(" FROM SPL.VW_SYNC_CONTRACTS ");
			_strQuery.append(" WHERE  ");			
			_strQuery.append(" (PROJECT = 'SGVJL' AND DOCUMENT_NO = 'CW-1303-000008' ");
			_strQuery.append(" OR PROJECT = 'SSL' AND DOCUMENT_NO = 'CW-1302-000016' ");
			_strQuery.append(" OR PROJECT = 'VCM' AND DOCUMENT_NO = 'CW-1207-000002' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000036' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000048' ");
			_strQuery.append(" OR PROJECT = 'BY' AND DOCUMENT_NO = 'CW-1302-000005' ");
			_strQuery.append(" OR PROJECT = 'SVP52' AND DOCUMENT_NO = 'CW-1210-000007' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1210-000032' ");
			_strQuery.append(" OR PROJECT = 'PT' AND DOCUMENT_NO = 'CW-1210-000003' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1209-000003' ");
			_strQuery.append(" OR PROJECT = 'SPVRS' AND DOCUMENT_NO = 'CW-1301-000026' ");
			_strQuery.append(" OR PROJECT = 'SPV-P' AND DOCUMENT_NO = 'CW-1210-000002' ");
			_strQuery.append(" OR PROJECT = 'SPVR5' AND DOCUMENT_NO = 'CW-1210-000016' ");
			_strQuery.append(" OR PROJECT = 'VSP' AND DOCUMENT_NO = 'CW-1210-000009' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1301-000005' ");
			_strQuery.append(" OR PROJECT = 'PVD' AND DOCUMENT_NO = 'CW-1210-000004' ");
			_strQuery.append(" OR PROJECT = 'SGV' AND DOCUMENT_NO = 'CW-1212-000018' ");
			_strQuery.append(" OR PROJECT = 'VP' AND DOCUMENT_NO = 'CW-1209-000003' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000011' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1210-000020' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000010' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000016' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000008' ");
			_strQuery.append(" OR PROJECT = 'NV5' AND DOCUMENT_NO = 'CW-1205-000001' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000014' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CW-1209-000009' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000059' ");
			_strQuery.append(" OR PROJECT = 'VRAM' AND DOCUMENT_NO = 'CW-1209-000004' ");
			_strQuery.append(" OR PROJECT = 'VRAM' AND DOCUMENT_NO = 'CW-1209-000003' ");
			_strQuery.append(" OR PROJECT = 'SPVOR' AND DOCUMENT_NO = 'CW-1212-000013' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1210-000012' ");
			_strQuery.append(" OR PROJECT = 'VRAM' AND DOCUMENT_NO = 'CW-1209-000002' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000008' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000049' ");
			_strQuery.append(" OR PROJECT = 'SPVOR' AND DOCUMENT_NO = 'CW-1301-000004' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1302-000052' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000046' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000055' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1211-000008' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000057' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000056' ");
			_strQuery.append(" OR PROJECT = 'SGV' AND DOCUMENT_NO = 'CW-1212-000012' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000065' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000053' ");
			_strQuery.append(" OR PROJECT = 'SPV-P' AND DOCUMENT_NO = 'CW-1209-000002' ");
			_strQuery.append(" OR PROJECT = 'SGV' AND DOCUMENT_NO = 'CW-1212-000011' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000038' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000054' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000034' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000027' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000062' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000026' ");
			_strQuery.append(" OR PROJECT = 'SGV-L' AND DOCUMENT_NO = 'CW-1210-000008' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1302-000125' ");
			_strQuery.append(" OR PROJECT = 'SPV-P' AND DOCUMENT_NO = 'CW-1209-000006' ");
			_strQuery.append(" OR PROJECT = 'SGV' AND DOCUMENT_NO = 'CW-1212-000006' ");
			_strQuery.append(" OR PROJECT = 'SGVP' AND DOCUMENT_NO = 'CW-1212-000021' ");
			_strQuery.append(" OR PROJECT = 'SGVP' AND DOCUMENT_NO = 'CW-1301-000001' ");
			_strQuery.append(" OR PROJECT = 'SGVP' AND DOCUMENT_NO = 'CW-1301-000003' ");
			_strQuery.append(" OR PROJECT = 'NV5' AND DOCUMENT_NO = 'CW-1209-000016' ");
			_strQuery.append(" OR PROJECT = 'SGV5' AND DOCUMENT_NO = 'CW-1209-000005' ");
			_strQuery.append(" OR PROJECT = 'SGVP' AND DOCUMENT_NO = 'CW-1211-000009' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1205-000002' ");
			_strQuery.append(" OR PROJECT = 'VP' AND DOCUMENT_NO = 'CW-1302-000003' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1210-000001' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1210-000028' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1211-000009' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1211-000031' ");
			_strQuery.append(" OR PROJECT = 'NVOL' AND DOCUMENT_NO = 'CT-1212-000001' ");
			_strQuery.append(" OR PROJECT = 'SSL' AND DOCUMENT_NO = 'CT-1212-000001' ");
			_strQuery.append(" OR PROJECT = 'VSK' AND DOCUMENT_NO = 'CT-1212-000001' ");
			_strQuery.append(" OR PROJECT = 'SGVPO' AND DOCUMENT_NO = 'CT-1301-000001' ");
			_strQuery.append(" OR PROJECT = 'SSL' AND DOCUMENT_NO = 'CT-1208-000003' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1206-000001' ");
			_strQuery.append(" OR PROJECT = 'PVD' AND DOCUMENT_NO = 'CW-1209-000012' ");
			_strQuery.append(" OR PROJECT = 'PT' AND DOCUMENT_NO = 'CW-1301-000031' ");
			_strQuery.append(" OR PROJECT = 'SVLA' AND DOCUMENT_NO = 'CW-1210-000021' ");
			_strQuery.append(" OR PROJECT = 'VP48' AND DOCUMENT_NO = 'CW-1212-000009' ");
			_strQuery.append("     )          ");					                
			_strQuery.append(" AND STATUS IN ('A','Z','C', 'X') ");  
			_strQuery.append(" AND APPROVED_BY IS NOT NULL  ");
			_strQuery.append(" GROUP BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON "); 
			_strQuery.append(" ORDER BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON  ");
*/
			
			List<Object[]> result = (List<Object[]>) sessionFactory.CreateEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			System.out.println("result size =" + CommonService.getSize(result));
			
			if(!CommonService.IsNull(result)){
				try{
					for (Object[] value : result) {	
						CleanValue();
						setAction(CommonService.IsNullRNull(value[3]));
						setCompany(CommonService.IsNullRNull(value[0]));
						setProject(CommonService.IsNullRNull(value[1]));
						setDocumentNo(CommonService.IsNullRNull(value[2]));
						setStatus(CommonService.IsNullRNull(value[3]));
						setSyncIsstaging(CommonService.IsNullRNull(value[4]));
						setProcessAction(QueueProcessAction.BATCH);
						setProcessActionBy(CommonService.IsNullRNull(value[7]));
						new apps.promis.out.common.SyncQueueAction().PutQueue(
																				promis.utility.BusinessModule.CONSTRUCTION_LOW_RISE.toString(), 
																				promis.utility.ProcessModule.CONSTRUCTION_CONTRACT_LOW_RISE.toString(),
																				null,
																				QueueProcessAction.BATCH,
																				ConvertParamToHashMap()
																			 );
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			
			new ContractBatch().BatchExec();
			
		}catch(Exception e){
			log.error(e.getMessage());
		}

	}

}
